﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;
using NHibernate.Linq;
using FluentNHibernate.Testing;


namespace InnovTech.Data.NHibernate.Test
{

    

    [TestClass]
    public class TestTestDB
    {

        [TestInitialize]
        public void TestInit()
        {

        }

        [TestCleanup]
        public void TestCleanup()
        {

        }

        [TestMethod]
        public void TestMethod1()
        {
            
            ISessionFactory factory = TestUtil.CreateTestDB();
            ISession session = factory.OpenSession();

            IEnumerable<Person> persons;
            Person p1 = new Person() { FirstName = "aaa", LastName = "bbb" };
            Person p2 = new Person() { FirstName = "111", LastName = "222", Friend = p1};
            p2.Pets = new List<Pet>();
            
            Pet pet1 = new Pet() { Name = "Pet 1", Owner = p2 };
            p2.Pets.Add(pet1);
            Pet pet2 = new Pet() { Name = "Pet 2", Owner = p2 };
            p2.Pets.Add(pet2);

            using (var tx = session.BeginTransaction())
            {
                persons = session.Query<Person>().Where( p => true);
                tx.Commit();
            }
            Assert.AreEqual(0, persons.Count());

            using (var tx = session.BeginTransaction())
            {
                session.Save(p1);
                session.Save(p2);
                tx.Commit();
            }

            IList<Pet> pets;
            using (var tx = session.BeginTransaction())
            {
                pets = session.CreateCriteria(typeof(Pet)).List<Pet>();
                tx.Commit();
            }

            Assert.AreEqual(2, pets.Count);

            using (var tx = session.BeginTransaction())
            {
                persons = session.Query<Person>().Where(p => true);
                tx.Commit();
            }
            Assert.AreEqual(2, persons.Count());

            using (var tx = session.BeginTransaction())
            {
                session.Evict(p2);
                tx.Commit();
            }
            

            Person p_2 = session.Load<Person>(p2.ID, LockMode.None);
            //using (var tx = session.BeginTransaction())
            //{
            //    persons = session.Query<Person>().Where(p => p.ID.Equals(p2.ID));
            //    tx.Commit();
            //}
            //Assert.AreEqual(1, persons.Count());

            //p2 = persons.First();

            Assert.AreEqual(p1, p_2.Friend);
            Assert.AreEqual(2, p_2.Pets.Count);

            p_2.Friend.Friend = p_2;

            using (var tx = session.BeginTransaction())
            {
                session.Save(p_2);
                tx.Commit();
            }

            using (var tx = session.BeginTransaction())
            {
                session.Evict(p1);
                tx.Commit();
            }

            Person p_1 = session.Load<Person>(p1.ID, LockMode.None);

            Assert.AreEqual(p_2, p_1.Friend);

            TestUtil.CloseTestDB();

        }
    }
}
